package unit8.part4;

import javax.swing.*;
import java.awt.*;

public class JTableAndJScrollPaneTest {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("测试表格和滚动窗格");
        jFrame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        jFrame.setSize(600, 600);

        String[] columnNames = {"姓名", "得分"};
        String[][] rowData = new String[30][2];
        for (int i = 0; i < 30; i++) {
            char a = (char) ('A' + i);
            rowData[i] = new String[]{String.valueOf(a), 100 - i + ""};
        }

        JTable jTable = new JTable(rowData, columnNames);// 表格

        // 如何配置
        jTable.setPreferredSize(new Dimension(500, 500));// 设置大小
        jTable.setGridColor(Color.BLUE);// 设置网格线颜色
        jTable.setBackground(Color.CYAN);// 设置背景色
        jTable.setForeground(Color.RED);// 设置前景色

        // 如何查询
        for (int i = 0; i < jTable.getRowCount(); i++) {
            for (int j = 0; j < jTable.getColumnCount(); j++) {
                jTable.getCellEditor(i, j).getCellEditorValue();
            }
        }

        // 加入顺序
        JScrollPane scrollPane = new JScrollPane(jTable);// 表格加入到滚动窗格
        jFrame.add(scrollPane);// 滚动窗格加入到窗体

        jFrame.setVisible(true);

        // 如何修改添加
        rowData[0][0] = "张震";
        jTable.repaint();
    }
}
